<?php
/* this file will handle coupon code verification */
// load wordpress defaults, make global variables/functions available 
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
header( 'HTTP/1.1 200 OK' );

// local stuff
$ccode = !empty( $_POST[ 'couponcode' ] ) ? stripslashes( strip_tags( urldecode( $_POST[ 'couponcode' ] ) ) ) : NULL; // coupon code sent

// options
$aOptions = SofaSuppaStore::initOptions( false );
$checkout_voucher = stripslashes( strip_tags( $aOptions[ 'checkout_voucher' ] ) );

// setupz
$is_found = 0;
$arr_vouchers = explode( "\n", $checkout_voucher );

if( $arr_vouchers && wp_verify_nonce( $_POST[ 'sofa_suppastore_cc' ], 'chck_ref_cc' ) ) {
	foreach( $arr_vouchers as $k => $v ) {
		$arr_vouchers[ $k ] = trim( $v ); // trim whitespace
		$unit_obj = explode( '*', $arr_vouchers[ $k ] );
		if( $unit_obj[ 0 ] == $ccode ) {
			$is_found = floatval( $unit_obj[ 1 ] );
			break;
		}
	}
} else echo 0;

// response
if( $is_found ) echo $is_found;
else echo 0;